package com.xds.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xds.common.valif.AddGroup;
import com.xds.common.valif.UpdateGroup;
import com.xds.common.valif.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.xds.gulimall.product.entity.BrandEntity;
import com.xds.gulimall.product.service.BrandService;
import com.xds.common.utils.PageUtils;
import com.xds.common.utils.R;


/**
 * 品牌
 *
 * @author baixu
 * @email baixu@gmail.com
 * @date 2021-11-09 17:16:06
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
   // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }
    @GetMapping("/infos")
    public R info(@RequestParam("brandIds")List<Long> brandIds){
      List<BrandEntity> brandEntities=  brandService.getBrandsByIds(brandIds);
      return R.ok().put("brand",brandEntities);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand){
//      if (result.hasErrors()){
//          Map<String,String> map=new HashMap();
//          result.getFieldErrors().forEach((item)->{
//              String message = item.getDefaultMessage();
//              String field = item.getField();
//              map.put(field,message);
//          });
//       return   R.error(400,"提交的数据非法").put("data",map);
//      }else {
//
//      }
        brandService.save(brand);


        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class})   @RequestBody BrandEntity brand){
        //同步更新其他关联表中的数据
		brandService.updateByIdDetail(brand);


        return R.ok();
    }
    /**
     * 快速修改状态
     */
    @RequestMapping("/update/status")
    // @RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated({UpdateStatusGroup.class})   @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
